﻿using CL.Business.BLL;
using CL.Business.DAL;
using CL.Game.BLL;
using CL.Game.BLL.Tools;
using CL.Tools.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace CL.Mobile
{
    public partial class Regester : System.Web.UI.Page
    {
        Log log = new Log("Regester");
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测是否为微信登陆
            if (Request.UserAgent.ToLower().Contains("micromessenger"))
            {
                var wechat = new WeChatHelper(this);
                wechat.redirecturl = HttpUtility.UrlEncode(this.Request.Url.ToString().Replace("Regester.aspx","app.html"));
                log.Write(string.Format("redirecturl:{0}", wechat.redirecturl));
                var userInfo = wechat.GetOAuthSnsApiUserInfo();
                if (userInfo != null)
                {
                    try
                    {
                        string uid = Request["extenCode"];
                        log.Write(string.Format("extenCode:{0}", uid));

                        long uuid = 0;
                        long.TryParse(DESEncrypt.Decrypt(uid), out uuid);
                        log.Write(string.Format("uuid:{0}", uuid));

                        string sign = Request["sign"];
                        log.Write(string.Format("sign:{0}", sign));
                
                        var rec = new UsersBLL().ActivtiyRegister_WX(uuid, userInfo.unionid, 0, userInfo.nickname, userInfo.headimgurl);
                        log.Write(Newtonsoft.Json.JsonConvert.SerializeObject(rec));
                    }
                    catch (Exception ex)
                    {
                        new Log("Regester").Write(string.Format("参数读取失败:{0}", ex));
                    }
                }
            }
            else if (HttpContext.Current.Request.RequestType == "POST")
            {
                string phoneNum = Request["userPhone"].ToString();
                string codeNum = Request["phoneCheck"].ToString();
                string passWord = Request["userPassword"].ToString();
                string extenCode = Request["extenCode"].ToString();

                if(string.IsNullOrEmpty(phoneNum) || string.IsNullOrEmpty(codeNum) || string.IsNullOrEmpty(passWord))
                {
                    ErrorMsg("参数不对!");
                }

                var ret = new UsersBLL().MobileRegister("web", 0, phoneNum, passWord, codeNum);
                if(0 == ret.Code)
                {
                    if(!string.IsNullOrEmpty(extenCode))
                    {
                        var bll = new AgencyUserBLL();
                        bll.BindAgentAndUser(extenCode, ret.Data.UserCode.ToString());
                    }
                    Response.Write("<script >alert(\"" + "注册成功!" + "\");location.href='/app.html';</script>");
                }
                else
                {
                    ErrorMsg(ret.Msg);
                }
            }
        }


        protected void ErrorMsg(string eMsg)
        {
            string extenCode = Request["extenCode"].ToString();
            if (!string.IsNullOrEmpty(extenCode))
                extenCode = "?sid=" + extenCode;
            else
                extenCode = "";
            Response.Write("<script >alert(\"" + eMsg + "\");location.href='/Regester.aspx" + extenCode + "';</script>");
            Response.End();
        }
    }
}